﻿using System;
using System.Data;
using System.Text;
using ZHDJ.Core.Caching;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Forum;
using ZHDJ.WebPage;

namespace ZHDJ.Web
{
    public partial class Default : PortalPage
    {
        public struct DefaultPageCacheKey
        {
            public const string PhotoWallCacheKey = "PARTY_DefaultPage_PhotoWall_DATASOURCE";
            public const string RankMemberCacheKey = "PARTY_DefaultPage_RankMember_DATASOURCE";
            public const string RankBranchCacheKey = "PARTY_DefaultPage_ RankBranch_DATASOURCE";
        }

        MemoryCacheManager CacheManager = new MemoryCacheManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                LoadData();
            }
        }

        private void LoadData()
        {
            //轮播图片
            DataTable articleImageSource = PartyArticle.GetPortalArticleImage();
            for (int i = 1, len = articleImageSource.Rows.Count; i <= len; i++)
            {
                ltNumber.Text += "<li class=\"\">" + i + "</li>";
            }
            rpArticleImage.DataSource = articleImageSource;
            rpArticleImage.DataBind();

            //工作动态
            rpWorkDynamics.DataSource = PartyArticle.GetArticleTitle("", "DynamicWork", "", "limit 0,8");
            rpWorkDynamics.DataBind();

            //通知公告
            rpNotificationAnnouncement.DataSource = PartyArticle.GetArticleTitle("", "Announcement", "", "limit 0,8");
            rpNotificationAnnouncement.DataBind();

            //党务公开
            rpPartyWork.DataSource = PartyArticle.GetArticleTitle("", "PartyBuildingWork", "PartyOpen", "limit 0,6").Tables[0];
            rpPartyWork.DataBind();

            //基层党建
            rpGrassRoots.DataSource = PartyArticle.GetArticleTitle("", "PartyBuildingWork", "Basic", "limit 0,6").Tables[0];
            rpGrassRoots.DataBind();

            //政策法规
            rpPolicy.DataSource = PartyArticle.GetArticleTitle("", "PartyBuildingWork", "Policy", "limit 0,6").Tables[0];
            rpPolicy.DataBind();

            //办事指南
            rpGuid.DataSource = PartyArticle.GetArticleTitle("", "PartyBuildingWork", "Guide", "limit 0,6").Tables[0];
            rpGuid.DataBind();

            //机关建设
            rpInstitution.DataSource = PartyArticle.GetArticleTitle("", "OfficeBuilding", "", "limit 0,6").Tables[0];
            rpInstitution.DataBind();

            //反腐倡廉
            rpCorruption.DataSource = PartyArticle.GetArticleTitle("", "AntiCorruption", "", "limit 0,6").Tables[0];
            rpCorruption.DataBind();

            //人才天地
            rpTalentWorld.DataSource = PartyArticle.GetArticleTitle("", "TalentWorld", "", "limit 0,6").Tables[0];
            rpTalentWorld.DataBind();

            //党员排名
            rpRankMember.DataSource = CacheManager.Get<DataSet>(DefaultPageCacheKey.RankMemberCacheKey, () =>
            {
                return PartyScoreHelper.GetAllRankMember(0, 10);
            });
            rpRankMember.DataBind();

            //组织排名
            rpRankBranch.DataSource = CacheManager.Get<DataSet>(DefaultPageCacheKey.RankBranchCacheKey, () =>
            {
                return PartyScoreHelper.GetAllRankBranch(0, 10);
            });
            rpRankBranch.DataBind();
            //留影墙
            this.rptHonorHall.DataSource = CacheManager.Get<DataTable>(DefaultPageCacheKey.PhotoWallCacheKey, () =>
            {
                return PartyPhotoWallImage.GetPortalPhotoWallList(10);
            });
            this.rptHonorHall.DataBind();

            //志愿者召集活动
            rpVolunteerActivitieCall.DataSource = PartyVolunteerActivitie.GetActiviteTitle("", "limit 0,6");
            rpVolunteerActivitieCall.DataBind();

            //志愿者心语
            rpVolunteerQuotation.DataSource = PartyVolunteerQuotation.GetQuotationTitle("", "limit 0,6");
            rpVolunteerQuotation.DataBind();

            //论坛
            rptForum.DataSource = PartyForum.GetGridData(0, 13, "");
            rptForum.DataBind();

            //党内招聘
            rpJobHelping.DataSource = PartyArticle.GetArticleTitle("", "partylove", "PartyRecruit", "limit 0,6").Tables[0];
            rpJobHelping.DataBind();

            //关爱信息
            this.rpLoveMessage.DataSource = PartyArticle.GetArticleTitle("", "partylove", "CareInfoCourier", "limit 0,6").Tables[0];
            this.rpLoveMessage.DataBind();

            ///高层聚焦
            var gaocengjujiao= PartyArticle.GetArticleTitle("", "partylove", "CareInfoCourier", "limit 0,6").Tables[0];

            //飘窗
            this.FloatWindow();
        }

        /// <summary>
        /// 截取标题
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string CutLongTitle(object obj, int length)
        {
            string title = Convert.ToString(obj);
            if (title.Length > length)
            {
                title = title.Substring(0, length) + "...";
            }
            return title;
        }

        #region 首页飘窗
        /// <summary>
        /// 飘窗
        /// </summary>
        public void FloatWindow()
        {
            DataTable dt = PartyFloatWindow.GetSuoyeData().Tables[0];
            StringBuilder sb = new StringBuilder();
            string temp = @" <div id='Div{0}' class='souyewindowsshow' style='z-index: 9999;' position='{4}'>
            <a href='{1}' {5} >
                <img src='{2}' style='border: 0;'
                    title='{3}' alt='{3}' /></a><br />
            <a id='A{0}' class='piaochuangClose' style='cursor: pointer; color: #818181'>关闭</a>
        </div>";
            foreach (DataRow dr in dt.Rows)
            {
                var herf = "PortalPageRank.aspx?ID=" + dr["ID"];
                if (Convert.ToString(dr["ContentType"]) == "1")
                {
                    herf = Convert.ToString(dr["ContentUrl"]);
                }
                var tag = "target='_blank'";
                if (herf == "" || herf.Trim() == "#")
                {
                    tag = "";
                    herf = "javascript:void(0);";
                }
                sb.AppendFormat(temp, dr["ID"], herf, UpFileBusiness.GetFileUrl(Convert.ToString(dr["ImgUrl"])), dr["Title"], dr["Position"], tag);
            }
            if (!string.IsNullOrEmpty(sb.ToString()))
            {
                this.litwindowsshow.Text = sb.ToString();
            }
        }
        #endregion
    }
}